'use strict';

import React from 'react';
import { Dialog,Button,Loading  } from 'qnui';
import { Row, Col } from 'qnui/lib/grid';
import './index.scss';

class SystemDialog extends React.Component {
	constructor(props) {
        super(props);
        this.state = {
        		refreshDialog:false,
        		refreshContent:"",
        		creatingDialog:false,
        		}
        }
	onRefreshClose(){
		this.setState({"refreshDialog":false,"orderDialog":false});
		if(typeof (this.props.onCloseDialog)=="function"){
			this.props.onCloseDialog();
		}
	}	
	onClose(){
		this.setState({"creatingDialog":false});
		if(typeof (this.props.onCloseDialog)=="function"){
			this.props.onCloseDialog();
		}
	}
	componentWillReceiveProps(nextProps){
		this.setState({
			creatingDialog:(nextProps.creating?true:nextProps.error),
			refreshDialog:nextProps.orderDialog?nextProps.orderDialog:nextProps.refreshDialog,
			refreshContent:nextProps.refreshContent?nextProps.refreshContent:{msg:"",content:""}
			});
		if(nextProps.refreshDialog){
			let _self = this;
			// window.parent.closeRefreshDialog();  iframe页面调用该关闭方式
			window.closeRefreshDialog = (info)=>{
				window.info = info; //刷新已授权的信息
				_self.setState({"refreshDialog":false});
			}
		}
	}
  render() {	
		const loginBtn = this.props.loginError?
										<Button 
											size="large" 
											type="primary" 
											component="a" href="https://oauth.taobao.com/authorize?response_type=code&client_id=21127968&redirect_uri=http://llxl.hz.taeapp.com/?state=zhekou1_list&view=web"
										>
											<span>请重新登陆</span>
										</Button>
			 :"";
	const footer = this.props.loginError?<div>{loginBtn}</div>:"";
	const creatingTitle = this.props.creating?"正在读取。。。":"错误信息";
	const creatingMsg = this.props.creating?<Loading />:this.props.message;
	let dialogContent;
	if(this.props.refreshDialog){
		dialogContent={
				title:"授权超时",
				msg:"请授权并在30分钟内完成创建折扣",
				content:<iframe name="showLogin" id="showLogin" style={{border:"none",height:"420px",width:"380px"}}  scrolling="no" src="https://oauth.taobao.com/authorize?client_id=21127968&redirect_uri=http%3A%2F%2Fllxl.hz.taeapp.com%2F?a=refresh&response_type=code&state=llxl.hz.taeapp.com.21127968&view=web" />
		}
	}else{
		dialogContent={
				title:"请续费",
				msg:"订购时间不足，活动截止时间不能晚于订购时间",
				content:<a target="_blank" href="https://fuwu.taobao.com/ser/detail.htm?service_code=FW_GOODS-1840332"><Button type="primary" size="large" shape="warning">续费</Button></a>
		}
	}
	const refreshFooter = <div></div>;
    return (
		<div>
			<Dialog
					visible = {this.state.refreshDialog}
			      onOk = {this.onRefreshClose.bind(this)}
			      onCancel = {this.onRefreshClose.bind(this)}
			      onClose = {this.onRefreshClose.bind(this)} 
					title = {dialogContent.title}
					style={{backgroundColor:"#F0F0F0"}}
					footer = {refreshFooter}
					>
					<Row justify="center">
						{dialogContent.msg}
					</Row>
					<Row justify="center">
						{dialogContent.content}
					</Row>
			</Dialog>
			<Dialog style={{width:"300px"}} 
					visible = {this.state.creatingDialog}
			      onOk = {this.onClose.bind(this)}
			      onCancel = {this.onClose.bind(this)}
			      onClose = {this.onClose.bind(this)} 
					title = {creatingTitle}
					footer = {footer}
					>
					<Row justify="center">
						<h3>{creatingMsg}</h3>
					</Row>
			</Dialog>
		</div>
    );
  }
}

export default SystemDialog;
